package com.tiger.auth;

import org.apache.commons.lang3.StringUtils;
import org.jasig.cas.client.util.AssertionHolder;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.utils.Conf;
import com.tiger.web.BaseApi;
import com.tiger.web.SessionUtils;


/**
 * 登录Api
 * 
 * @author andy
 * 
 */
@Controller
@RequestMapping("/api/v1")
public class LoginApi extends BaseApi {
	
	
	@RequestMapping(value = "/admins/me", method = RequestMethod.GET)
	@ResponseBody
	public Object getMyAdminInfo() {
		Operator me = Operator.me();
		if(me == null) {
			return notFound("");
		}
		return me;
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(String from) {
		Assertion assertion = AssertionHolder.getAssertion();
		String userId = (String)assertion.getPrincipal().getAttributes().get("userId");
		String name = assertion.getPrincipal().getName();
		SessionUtils.set(Operator.SESSION_OPERATOR_KEY, new Operator(userId, name));
		if(StringUtils.isBlank(from)) {
			from = Conf.get("service.url");
		}
		return "redirect:" + from;
	}
	
}
